package com.example.project.controller.project;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.project.annotation.Log;
import com.example.project.core.controller.BaseController;
import com.example.project.core.domain.AjaxResult;
import com.example.project.core.page.TableDataInfo;
import com.example.project.entity.IssueCategorie;
import com.example.project.entity.Tracker;
import com.example.project.enums.BusinessType;
import com.example.project.service.IssueCategorieService;
import com.example.project.service.TrackerService;

/**
 * 配置管理
 */
@Controller
@RequestMapping("/project/setting")
public class ProjectConfigController extends BaseController {

	private String prefix = "pm/setting";

	@Autowired
	private TrackerService trackersService;
	
	@Autowired
	private IssueCategorieService issueCategoriesService;

	@RequiresPermissions("project:setting:view")
	@GetMapping("index")
	public String index(ModelMap mmap) {

		// 跟踪列表
		List<Tracker> trackerList = trackersService.findAllList();
		mmap.put("trackerList", trackerList);
		return prefix + "/setting_index";
	}

	/**
	 * 新增跟踪类型
	 */
	@RequiresPermissions("project:setting:add")
	@GetMapping("/addTracker")
	public String addTracker() {
		return prefix + "/tracker_add";
	}

	/**
	 * 新增保存跟踪类型
	 */
	@RequiresPermissions("system:trackers:add")
	@Log(title = "跟踪类型", businessType = BusinessType.INSERT)
	@PostMapping("/saveTracker")
	@ResponseBody
	public AjaxResult saveTracker(Tracker tTrackers) {
		return toAjax(trackersService.insertTTrackers(tTrackers));
	}

	/**
	 * 更新跟踪类型是否显示在变更日志中
	 */
	@RequiresPermissions("system:trackers:edit")
	@Log(title = "跟踪类型", businessType = BusinessType.UPDATE)
	@PostMapping("/updateIsInChlog")
	@ResponseBody
	public AjaxResult updateIsInChlog(String trackerIds) {
		return toAjax(trackersService.updateIsInChlog(trackerIds));
	}

	/**
	 * 获取问题类别列表
	 */
	@RequiresPermissions("project:setting:list")
	@PostMapping("/categories/list")
	@ResponseBody
	public TableDataInfo categoriesList(IssueCategorie issueCategories) {
		startPage();
		List<IssueCategorie> list = issueCategoriesService.selectIssueCategorieList(issueCategories);
		return getDataTable(list);
	}

	/**
	 * 新增问题类别
	 */
	@RequiresPermissions("project:setting:add")
	@GetMapping("/categories/add")
	public String add() {
		return prefix + "/issueCategories_add";
	}

	/**
	 * 新增保存问题类别
	 */
	@RequiresPermissions("project:setting:add")
	@Log(title = "问题类别", businessType = BusinessType.INSERT)
	@PostMapping("/categories/add")
	@ResponseBody
	public AjaxResult addSave(IssueCategorie issueCategories) {
		return toAjax(issueCategoriesService.insertIssueCategorie(issueCategories));
	}

	/**
	 * 修改问题类别
	 */
	@RequiresPermissions("project:setting:edit")
	@GetMapping("/categories/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap) {
		IssueCategorie issueCategories = issueCategoriesService.selectIssueCategorieById(id);
		mmap.put("issueCategories", issueCategories);
		return prefix + "/issueCategories_edit";
	}

	/**
	 * 修改保存问题类别
	 */
	@RequiresPermissions("project:setting:edit")
	@Log(title = "问题类别", businessType = BusinessType.UPDATE)
	@PostMapping("/categories/edit")
	@ResponseBody
	public AjaxResult editSave(IssueCategorie issueCategories) {
		return toAjax(issueCategoriesService.updateIssueCategorie(issueCategories));
	}

	/**
	 * 删除问题类别
	 */
	@RequiresPermissions("project:setting:remove")
	@Log(title = "问题类别", businessType = BusinessType.DELETE)
	@PostMapping("/categories/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(issueCategoriesService.deleteIssueCategorieByIds(ids));
	}

}
